package com.audaque.springboot.foshanupload.run.listener;

import cn.hutool.core.util.StrUtil;
import com.audaque.springboot.foshanupload.core.util.ApplicationContextUtils;
import com.audaque.springboot.foshanupload.run.properties.ServerinfoProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author zgb
 * @desc ...
 * @date 2022-01-22 15:00:02
 */

@Component
@Slf4j
public class ApplicationReadyEventListener implements ApplicationListener<ApplicationEvent> {


    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {


        if (applicationEvent instanceof ApplicationReadyEvent) {
            log.debug("ApplicationReadyEvent:项目启动成功！");

            String ip = null;
            try {
                ip = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException e) {
                throw new RuntimeException(e);
            }
            ServerinfoProperties.ip = ip;
            log.debug("ip:" + ip);
            ApplicationContext applicationContext = ApplicationContextUtils.getApplicationContext();
            Environment env = applicationContext.getEnvironment();
            String port = env.getProperty("server.port");
            ServerinfoProperties.serverPort = Integer.parseInt(port);
            log.debug("serverPort:" + port);
            String path = null;
            String contextPath = env.getProperty("server.servlet.context-path");
            String basePath = env.getProperty("spring.reactor.base-path");
            if (StringUtils.isNotBlank(contextPath)) {
                path = contextPath;
            } else {
                path = StrUtil.SLASH;
            }
            ServerinfoProperties.applicationContextPath = path;
            log.debug("path:" + path);

            String serverUrl = "http://" + ip + ":" + port + path;
            ServerinfoProperties.serverUrl = serverUrl;
            //程序启动时间(秒)
            Long time = (System.currentTimeMillis() - ManagementFactory.getRuntimeMXBean().getStartTime()) / 1000;

            String serverInfo = "\n" +
                    "<----------------------< Application is running! >---------------------->\n" +
                    "\n" +
                    "\tStart Time: {}s\n" +
                    "\tAccess URL: {}\n" +
                    "-------------------------------------------------------------------------\n";

            log.debug(serverInfo, time, serverUrl);

            String managementServerPort = env.getProperty("management.server.port");
            log.debug("managementServerPort:" + managementServerPort);
            ServerinfoProperties.managementServerPort = Integer.parseInt(managementServerPort);

            String managementUrl = "http://" + ip + ":" + managementServerPort + "/actuator";
            ServerinfoProperties.managementUrl = managementUrl;
            log.debug("managementUrl:" + managementUrl);
            String managementInfo = "\n" +
                    "<----------------------< management is running! >---------------------->\n" +
                    "\n" +
                    "\tAccess URL: {}\n" +
                    "-------------------------------------------------------------------------\n";
            log.debug(managementInfo, managementUrl);


            String devtoolsLivereloadPort = env.getProperty("spring.sedevtoolsver.livereload.port");
            ServerinfoProperties.managementServerPort = Integer.parseInt(managementServerPort);
            log.debug("devtoolsLivereloadPort:" + devtoolsLivereloadPort);

        }

    }
}
